﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace TfsBranchLib2.Model
{
    public class BranchSetContainerStorage
    {
        private Dictionary<string, BranchSetPrecedentContainer> dic =
            new Dictionary<string, BranchSetPrecedentContainer>();


        private bool isLoaded;

        public void Load()
        {
            if (isLoaded) return;
            var serializationEventArgs = new XmlSerializationEventArgs();
            if (OnLoad != null) OnLoad(this, serializationEventArgs);
            else
            {
                dic = new Dictionary<string, BranchSetPrecedentContainer>();
                return;
            }
            var serializedData = serializationEventArgs.SerializedData;
            if (serializedData != null)
            {
                var serializer = new XmlSerializer(typeof (List<SerializationHelper>));
                var stream = new StringReader(serializedData);
                var list = (List<SerializationHelper>) serializer.Deserialize(stream);
                dic = new Dictionary<string, BranchSetPrecedentContainer>();
                if (list != null)
                {
                    foreach (var helper in list)
                    {
                        dic.Add(helper.TrunkPath, helper.BranchSetPrecedentContainer);
                    }
                }
            }
            else
            {
                dic = new Dictionary<string, BranchSetPrecedentContainer>();
            }
            isLoaded = true;
        }

        public void Save()
        {
            string serializedData;
            var serializer = new XmlSerializer(typeof (List<SerializationHelper>));
            var stream = new StringWriter();
            var list = new List<SerializationHelper>();
            foreach (var item in dic)
            {
                list.Add(new SerializationHelper {TrunkPath = item.Key, BranchSetPrecedentContainer = item.Value});
            }
            serializer.Serialize(stream, list);
            stream.Flush();
            serializedData = stream.ToString();
            if (OnSave != null) OnSave(this, new XmlSerializationEventArgs {SerializedData = serializedData});
        }

        public BranchSetPrecedentContainer GetContainer(string trunkPath)
        {
            BranchSetPrecedentContainer precedentContainer = null;
            if (dic == null)
            {
                Load();
            }
            dic.TryGetValue(trunkPath, out precedentContainer);
            if (precedentContainer == null)
            {
                precedentContainer = new BranchSetPrecedentContainer();
                dic.Add(trunkPath, precedentContainer);
            }
            return precedentContainer;
        }

        public event EventHandler<XmlSerializationEventArgs> OnSave;

        public event EventHandler<XmlSerializationEventArgs> OnLoad;

        public class XmlSerializationEventArgs : EventArgs
        {
            public string SerializedData { get; set; }
        }

        public class SerializationHelper
        {
            public string TrunkPath { get; set; }
            public BranchSetPrecedentContainer BranchSetPrecedentContainer { get; set; }
        }
    }
}